package com.yangle.frame.admin.controller;

import com.yangle.frame.admin.entity.FrameMenu;
import com.yangle.frame.admin.service.FrameMenuService;
import com.yangle.frame.common.controller.BaseController;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.util.StringUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by yangle on 2018/5/26.
 */
@Controller
@RequestMapping("/frame/menu")
public class FrameMenuController extends BaseController<FrameMenu> {
    @Autowired
    private FrameMenuService frameMenuService;

    @RequestMapping("/list")
    public String list(HttpServletRequest request, FrameMenu menu) {
        super.list(request, menu, frameMenuService);
        return "frame/menu/list";
    }

    @RequestMapping("/edit")
    public String edit(HttpServletRequest request, ModelMap map) {
        super.edit(request, frameMenuService);
        List<FrameMenu> mainMenus = frameMenuService.getMainMenu();
        request.setAttribute("mainMenus", mainMenus);
        return "frame/menu/edit";
    }

    @RequestMapping("/save")
    public String save(HttpServletRequest request, FrameMenu entity) {
        if (StringUtil.isNotEmpty(entity.getId())) {
            super.update(request, entity, frameMenuService);
        } else {
            entity.setId(UUID.randomUUID().toString());
            super.create(request, entity, frameMenuService);
        }
        return "common/result";
    }

    @RequestMapping("/delete")
    public String delete(HttpServletRequest request) {
        super.delete(request, frameMenuService);
        return "common/result";
    }

    @RequestMapping("/view")
    public String view(HttpServletRequest request) {
        super.view(request, frameMenuService);
        List<FrameMenu> mainMenus = frameMenuService.getMainMenu();
        request.setAttribute("mainMenus", mainMenus);
        return "frame/menu/view";
    }

    @ResponseBody
    @RequestMapping("/updateIsOpen")
    public Map updateIsOpen(String id, String isOpen) {
        frameMenuService.updateIsOpen(id, isOpen);
        Map map = new HashMap();
        map.put("status", 1);
        return map;
    }
}